package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierDao supplierDao;

    @Override
    public Map<String, Object> getSupplierList(Integer page, Integer rows, String supplierName) {
        Map<String, Object> map = new HashMap<>();

        Page<Object> page1 = PageHelper.startPage(page, rows);
        List<Supplier> supplierList = supplierDao.selectSupplierList(supplierName);

        map.put("total", page1.getTotal());
        map.put("rows", supplierList);
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Supplier supplier) {
        Integer supplierId = supplier.getSupplierId();
        //id不为空时 更新信息
        if (supplierId != null) {
            supplierDao.updateById(supplier);
            return;
        } else {
            //id 为空 新增数据
            supplierDao.saveSupplier(supplier);
        }
    }

    @Override
    public void deleteByIds(String ids) {
        String[] idsArr = ids.split(",");

        supplierDao.deleteByIds(idsArr);
    }
}
